package com.zhiyun.service.impl;

import com.zhiyun.domain.CourseUnit;
import com.zhiyun.mapper.CourseUnitMapper;
import com.zhiyun.service.CourseUnitService;
import com.zhiyun.util.Db;
import com.zhiyun.vo.Result;

import java.util.List;

/**
 * @author zc
 * @Date 2025/4/29 上午11:36
 */
public class CourseUnitServiceImpl implements CourseUnitService {
    CourseUnitMapper courseUnitMapper = Db.getMapper(CourseUnitMapper.class);
    @Override
    public Result findByCourseId(int courseId) {
        List<CourseUnit> list = courseUnitMapper.findByCourseId(courseId);
        return Result.ok(list);
    }

    @Override
    public Result insertCourseUnit(CourseUnit courseUnit) {
        int rows = courseUnitMapper.insert(courseUnit);
        if (rows > 0) {
            return Result.okMsg("添加成功");
        }
        return Result.fail("添加失败");
    }

    @Override
    public Result updateCourseUnit(CourseUnit courseUnit) {
        CourseUnit oldCourseUnit = courseUnitMapper.selectByPrimaryKey(courseUnit.getId());
        if (oldCourseUnit != null) {
            int rows = courseUnitMapper.updateByPrimaryKey(courseUnit);
            if (rows > 0) {
                return Result.okMsg("修改成功");
            }
            return Result.fail("修改失败");
        }else {
            return Result.fail("单元不存在");
        }
    }

    @Override
    public Result deleteCourseUnit(int courseUnitId) {
        CourseUnit oldCourseUnit = courseUnitMapper.selectByPrimaryKey(courseUnitId);
        if (oldCourseUnit != null) {
            int rows = courseUnitMapper.deleteByPrimaryKey(courseUnitId);
            if (rows > 0) {
                return Result.okMsg("删除成功");
            }else {
                return Result.fail("删除失败");
            }
        }else {
            return Result.fail("单元不存在");
        }
    }
}
